﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
using CoreSolution.Dto.Entity;
using CoreSolution.IService;
using CoreSolution.Shop.Api.Manger;
using CoreSolution.Tools.WebResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CoreSolution.Shop.Api.Controllers
{
    [Route("api/Menu")]
    [ApiController]
    public class MenuController : ControllerBase
    {

        private readonly IMenuService _IMenuService;
        private readonly IUserRoleService _UserRoleService;
        private readonly IMenuRoleService _IMenuRoleService;
      

        public MenuController(IMenuService menuController, IUserRoleService IUserRoleService, IMenuRoleService IMenuRoleService,
            IMenuService IMenuService)
        {
            _IMenuService = menuController;
            _UserRoleService = IUserRoleService;
            _IMenuRoleService = IMenuRoleService;
        }
        
        /// <summary>
        /// 获取菜单信息
        /// 获取菜单信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMenuInfo")]
        public JsonResult GetMenuInfo(Guid Id)
        {
            var result = _IMenuService.GetEntityDto(Id);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "成功", result);
        }
        
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMenuInfoList")]
        public JsonResult GetMenuInfoList()
        {
            var ListMenu=_IMenuService.GetMenuList(new List<Guid>());
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "成功", ListMenu);
        }

        /// <summary>
        /// 获取当前用户的菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetCurrentMenu")]
        public async Task<JsonResult> GetCurrentMenu()
        {
            string token = HttpContext.Request.Headers["X-Token"];
            var userId = (await LoginManager.GetUserIdAsync(token)).GetValueOrDefault();

            //获取用户菜单权限
            var RoleIdList = _UserRoleService.GetEntityDtoList(t => t.UserId == userId).Select(t => t.RoleId).Distinct().ToList();
            var MenuIdList = _IMenuRoleService.GetEntityDtoList(t => RoleIdList.Contains(t.RoleId)).Select(t => t.MenuId).Distinct().ToList();
            var MenuList = _IMenuService.GetMenuList(MenuIdList);
            
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "成功", MenuList);
        }

        /// <summary>
        /// 获取当前用户的菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetCurrentMenuList")]
        public async Task<JsonResult> GetCurrentMenuList()
        {
            string token = HttpContext.Request.Headers["X-Token"];
            var userId = (await LoginManager.GetUserIdAsync(token)).GetValueOrDefault();

            //获取用户菜单权限
            var RoleIdList = _UserRoleService.GetEntityDtoList(t => t.UserId == userId).Select(t => t.RoleId).Distinct().ToList();
            var MenuIdList = _IMenuRoleService.GetEntityDtoList(t => RoleIdList.Contains(t.RoleId)).Select(t => t.MenuId).Distinct().ToList();
            var MenuList = _IMenuService.GetEntityDtoList(t => MenuIdList.Contains(t.Id));

            return AjaxHelper.JsonResult(HttpStatusCode.OK, "成功", MenuList);
        }



    }
}